"""
    @project_name: Fishing_Calendar
    @author：henry_oulen@163.com
    @file： utils.py
    @date：2023/4/8 17:32
"""

import datetime
import json
import time
from datetime import datetime as dt

import requests
from bs4 import BeautifulSoup

from config.days_config import days


def get_dat(u_title=None):
    last_send = []
    if u_title:
        weekday = dt.strptime(u_title, "%Y-%m-%d").weekday()
    else:
        weekday = dt.now().weekday()
    if weekday == 0:
        _apd = "HI 搬砖人 【 今日周一 距离周末还有：5天 】"
        _a1 = "新的一周又开始了，新的压力又将降临。"
        _a2 = "有压力是好事，能够鞭策我们努力前行。"
        _a3 = "人生就是这样，没有如果，只有结果。"
        _a4 = "没有回头，只有往前走，一起加油!"
        _apc = "有事没事起身去茶水间，去厕所，去廊道走走。"
        _apcd = "别老在工位上坐着，钱是老板的，但命是自己的。"
        last_send.append(_apd)
        last_send.append("---------------------------")
        last_send.append(_a1)
        last_send.append(_a2)
        last_send.append(_a3)
        last_send.append(_a4)
        last_send.append(_apc)
        last_send.append(_apcd)
    elif weekday == 1:
        _apd = "HI 搬砖人 【 今日周二 距离周末还有：4天 】"
        _a1 = "惟有心怀光亮望向远方"
        _a2 = "才能看见幸福真正的模样"
        _a3 = "真正拥抱自己"
        _a4 = "更好的生活，正在前方等着你！"
        _apc = "有事没事起身去茶水间，去厕所，去廊道走走。"
        _apcd = "别老在工位上坐着，钱是老板的，但命是自己的。"
        last_send.append(_apd)
        last_send.append("---------------------------")
        last_send.append(_a1)
        last_send.append(_a2)
        last_send.append(_a3)
        last_send.append(_a4)
        last_send.append(_apc)
        last_send.append(_apcd)
    elif weekday == 2:
        _apd = "HI 搬砖人 【 今日周三 距离周末还有：3天 】"
        _a1 = "周三了，希望你有三头六臂，轻松完成工作。"
        _a2 = "三拳两脚，轻松打败困难。"
        _a3 = "吾日三省吾身，明白生活的意义。"
        _a4 = "三思而行，深思熟虑好好处理事情。"
        _a5 = "等周末再见你，定要让我刮目相看！"
        _apc = "有事没事起身去茶水间，去厕所，去廊道走走。"
        _apcd = "别老在工位上坐着，钱不是你的，但命是自己的。"
        last_send.append(_apd)
        last_send.append("---------------------------")
        last_send.append(_a1)
        last_send.append(_a2)
        last_send.append(_a3)
        last_send.append(_a4)
        last_send.append(_a5)
        last_send.append(_apc)
        last_send.append(_apcd)
    elif weekday == 3:
        _apd = "HI 搬砖人 【 今日周四 距离周末还有：2天 】"
        _a1 = "保留再多，也是一种缺失；"
        _a2 = "播撒多了，才会拥有。"
        _a3 = "吾日三省吾身，明白生活的意义。"
        _a4 = "三有舍才有得，与其有所保留，不如大方给予。"
        _a5 = "浇灌万物的同时，也浇灌了自己。"
        _apc = "有事没事起身去茶水间，去厕所，去廊道走走。"
        _apcd = "别老在工位上坐着，钱不是你的，但命是自己的。"
        last_send.append(_apd)
        last_send.append("---------------------------")
        last_send.append(_a1)
        last_send.append(_a2)
        last_send.append(_a3)
        last_send.append(_a4)
        last_send.append(_a5)
        last_send.append(_apc)
        last_send.append(_apcd)
    elif weekday == 4:
        _apd = "HI 搬砖人 【 今日周五 明天就是周六了！】"
        _a1 = "周一虐我千百遍，我待周五如初恋。"
        _a2 = "工作烦恼抛一边，快快乐乐似神仙。"
        _a3 = "三五朋友聚一聚，凑到一起不容易。"
        _a4 = "愿君有个愉快的周末!"
        _apc = "有事没事起身去茶水间，去厕所，去廊道走走。"
        _apcd = "别老在工位上坐着，钱不是你的，但命是自己的。"
        last_send.append(_apd)
        last_send.append("---------------------------")
        last_send.append(_a1)
        last_send.append(_a2)
        last_send.append(_a3)
        last_send.append(_a4)
        last_send.append(_apc)
        last_send.append(_apcd)
    elif weekday == 5:
        last_send.append("HI 搬砖人 【周六愉快】")
        last_send.append("---------------------------")
        _a1 = "周六又到，让太阳去早起，自己睡到笑。"
        _a2 = "让乌云去沉重，自己心情轻飘飘。"
        _a3 = "让风去匆忙，自己一边看风景，一边慢慢挪着脚"
        _a4 = "这是你的时间，尽情享受生活吧!"
        last_send.append(_a1)
        last_send.append(_a2)
        last_send.append(_a3)
        last_send.append(_a4)
    elif weekday == 6:
        last_send.append("HI 搬砖人 【周末愉快】")
        last_send.append("---------------------------")
        _a1 = "周末又到，让太阳去早起，自己睡到笑。"
        _a2 = "让乌云去沉重，自己心情轻飘飘。"
        _a3 = "让风去匆忙，自己一边看风景，一边慢慢挪着脚"
        _a4 = "这是你的时间，尽情享受生活吧!"
        last_send.append(_a1)
        last_send.append(_a2)
        last_send.append(_a3)
        last_send.append(_a4)
    last_send.append("---------------------------")
    for i in days:
        timeStamp = int(time.mktime(time.strptime(i.get("date"), "%Y-%m-%d")))
        if u_title:
            today = int(time.mktime(time.strptime(u_title, "%Y-%m-%d")))
        else:
            today = int(time.mktime(time.strptime(datetime.date.today().strftime("%Y-%m-%d"), "%Y-%m-%d")))
        day = (timeStamp - today) // (24 * 60 * 60)
        is_work = i.get("isWorkDay")
        strs = "距离【%s】 还有：%d天 %s" % (i.get("name"), day, "工作 " if is_work == 1 else "放假")
        if not str(day).startswith("-"):
            if day == 0:
                strss = "今天是%s 哟 祝你节日快乐" % (i.get("name"))
                last_send.append(strss)
            if i.get("important") == 1:
                last_send.append(strs)
    last_send.append("---------------------------")
    last_content = "认认真真上班，这根本就不叫赚钱，那是用劳动换取报酬。"
    _last_content = "人活半辈子才明白，真正高级的养生，是回家好好吃饭"
    _last_content0 = "别沮丧，生活就像心电图，一帆风顺就证明你挂了！"
    _last_content1 = "最后，祝愿天下所有摸鱼人，都能愉快的渡过每一天!"
    _last_content2 = "关注我，这样每天我都能见到你了"
    _last_content3 = "我是 【捣蛋小能手】 捣蛋的捣 & 捣蛋的蛋 ! "
    _last_content4 = "email:henry_oulen@163.com"

    last_send.append(last_content)
    last_send.append(_last_content)
    last_send.append(_last_content0)
    last_send.append(_last_content1)
    # last_send.append("---------------------------")
    # last_send.append(_last_content2)
    # last_send.append(_last_content3)
    content = json.dumps(last_send, ensure_ascii=False)
    #     #      综合   科技   娱乐    社区        购物      财经
    #     #page=['news','tech','ent','community','shopping','finance']
    # page = ['news']
    # try:
    #     for tag in page:
    #         url = 'https://tophub.today/c/{}'.format(tag)
    #         html = download_page(url)
    #         return_list = get_content(html, tag)
    #         last_send = last_send + return_list
    # except Exception as e:
    #     pass
    last_send.append("---------------------------")
    last_send.append(_last_content2)
    last_send.append(_last_content3)
    last_send.append(_last_content4)

    return last_send


def download_page(url):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"}
    try:
        r = requests.get(url, timeout=30, headers=headers)
        return r.text
    except:
        return "please inspect your url or setup"


def get_content(html, tag, _v):
    soup = BeautifulSoup(html, 'html.parser')
    con = soup.find('div', attrs={'class': 'bc-cc'})
    con_list = con.find_all('div', class_="cc-cd")
    return_list = []
    for i in con_list:
        num = []
        title = []
        hot = []
        href = []
        author = i.find('div', class_='cc-cd-lb').get_text()  # 获取平台名字
        if _v == "hot":
            if str(author.strip()) == "百度":
                _str = "------------%s--------------" % ("百度实时热榜" if author.strip() == "百度" else author.strip())
                return_list.append(_str)
                _num = 0
                link = i.find('div', class_='cc-cd-cb-l').find_all('a')  # 获取所有链接
                for k in link:
                    href.append(k['href'])
                    num.append(k.find('span', class_='s').get_text())
                    title.append(str(k.find('span', class_='t').get_text()))
                    hot.append(str(k.find('span', class_='e').get_text()))
                for h in range(len(num)):
                    _num += 1
                    _temp_str = "%s.%s" % (num[h], title[h])
                    return_list.append(_temp_str)
        elif _v == "today":
            if str(author.strip()) == "历史上的今天":
                _str = "------------%s--------------" % ("百度热榜" if author.strip() == "百度" else author.strip())
                return_list.append(_str)
                _num = 0
                link = i.find('div', class_='cc-cd-cb-l').find_all('a')  # 获取所有链接
                for k in link:
                    href.append(k['href'])
                    num.append(k.find('span', class_='s').get_text())
                    title.append(str(k.find('span', class_='t').get_text()))
                    hot.append(str(k.find('span', class_='e').get_text()))
                for h in range(len(num)):
                    _num += 1
                    _temp_str = "%s.%s" % (num[h], title[h])
                    return_list.append(_temp_str)
    return return_list


def save_txt(tag, *args):
    for i in args:
        with open(tag + '.txt', 'a', encoding='utf-8') as f:
            f.write(i)


def day_laster():
    last_send = get_dat()
    send = "------------节日倒数--------------\r\n"
    for i in last_send:
        send += i + "\r\n"
    # tkinter.messagebox.showinfo(title='my message', message=send)
    return send


def get_today():
    last_send = []
    #     #      综合   科技   娱乐    社区        购物      财经
    #     #page=['news','tech','ent','community','shopping','finance']
    page = ['news']
    _v = "today"
    try:
        for tag in page:
            url = 'https://tophub.today/c/{}'.format(tag)
            html = download_page(url)
            return_list = get_content(html, tag, _v)
            last_send = last_send + return_list
    except Exception as e:
        pass
    last_send.append("---------------------------")
    last_content = "认认真真上班，这根本就不叫赚钱，那是用劳动换取报酬。"
    _last_content = "人活半辈子才明白，真正高级的养生，是回家好好吃饭"
    _last_content0 = "别沮丧，生活就像心电图，一帆风顺就证明你挂了！"
    _last_content1 = "最后，祝愿天下所有摸鱼人，都能愉快的渡过每一天!"
    _last_content2 = "关注我，这样每天我都能见到你了"
    _last_content3 = "我是 【捣蛋小能手】 捣蛋的捣 & 捣蛋的蛋 ! "
    _last_content4 = "email:henry_oulen@163.com"

    last_send.append(last_content)
    last_send.append(_last_content)
    last_send.append(_last_content0)
    last_send.append(_last_content1)
    last_send.append(_last_content2)
    last_send.append(_last_content3)
    last_send.append(_last_content4)
    send = ""
    for i in last_send:
        send += i + "\r\n"
    return send
    # tkinter.messagebox.showinfo(title='my message', message=send)
    # last_print_text_answer.delete('1.0', END)
    # last_print_text_answer.insert(END, send, "center")  # ADDED


def get_hot_news():
    #     #      综合   科技   娱乐    社区        购物      财经
    #     #page=['news','tech','ent','community','shopping','finance']
    page = ['news']
    last_send = []
    _v = "hot"
    try:
        for tag in page:
            url = 'https://tophub.today/c/{}'.format(tag)
            html = download_page(url)
            return_list = get_content(html, tag, _v)
            last_send = last_send + return_list
    except Exception as e:
        pass
    last_send.append("---------------------------")
    last_content = "认认真真上班，这根本就不叫赚钱，那是用劳动换取报酬。"
    _last_content = "人活半辈子才明白，真正高级的养生，是回家好好吃饭"
    _last_content0 = "别沮丧，生活就像心电图，一帆风顺就证明你挂了！"
    _last_content1 = "最后，祝愿天下所有摸鱼人，都能愉快的渡过每一天!"
    _last_content2 = "关注我，这样每天我都能见到你了"
    _last_content3 = "我是 【捣蛋小能手】 捣蛋的捣 & 捣蛋的蛋 ! "
    _last_content4 = "email:henry_oulen@163.com"

    last_send.append(last_content)
    last_send.append(_last_content)
    last_send.append(_last_content0)
    last_send.append(_last_content1)
    last_send.append(_last_content2)
    last_send.append(_last_content3)
    last_send.append(_last_content4)
    send = ""
    for i in last_send:
        send += i + "\r\n"
    return send
    # tkinter.messagebox.showinfo(title='my message', message=send)
    # last_print_text_answer.delete('1.0', END)
    # last_print_text_answer.insert(END, send, "center")  # ADDED
